package cn.anecansaitin.whimsy.mixin.client;

import cn.anecansaitin.whimsy.common.register.ModEnchantments;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(LocalPlayer.class)
public abstract class LocalPlayerMixin {
    @Redirect(method = "aiStep", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/player/LocalPlayer;isUsingItem()Z", ordinal = 0))
    public boolean redirect$aiStep(LocalPlayer localPlayer) {
        if (localPlayer.isUsingItem()) {
            ItemStack item = localPlayer.getUseItem();

            if (item.getEnchantmentLevel(ModEnchantments.MANUAL_MOTOR_COORDINATION.get()) > 0) {
                return false;
            } else if (item.isEdible()){
                return localPlayer.getItemBySlot(EquipmentSlot.CHEST).getEnchantmentLevel(ModEnchantments.MANUAL_MOTOR_COORDINATION.get()) <= 0;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}
